package com.wzc.org.controller;

import com.wzc.basic.annotation.PreAuthorize;
import com.wzc.basic.exception.BusinessException;
import com.wzc.basic.query.BaseQuery;
import com.wzc.basic.util.AjaxResult;
import com.wzc.basic.util.ExcelUtils;
import com.wzc.basic.util.PageList;
import com.wzc.org.domain.Shop;
import com.wzc.org.domain.ShopLog;
import com.wzc.org.dto.ShopDto;
import com.wzc.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController   // @Controller 和
@RequestMapping("/shop")
@Api(value = "店铺接口类",description = "店铺相关CRUD")
public class ShopController {


    @Autowired
    private IShopService shopService;


    /**
     * 查询所有
     * @GetMapping
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
//    @PreAuthorize(name = "店铺管理查询所有", sn = "shop:list")
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }


    /**
     * 查询单个
     * @GetMapping("/{id}") 后端接收前端传输的数据（动态传输）
     * @PathVariable("id") 从路径上去获取id变量（后端取数据）再赋值给 Long id
     * @param id
     * @return
     */
    @GetMapping("/{id}")  // 接收参数 @PathVariable获取参数
    @ApiOperation(value = "通过id查询单个数据")
//    @PreAuthorize(name = "店铺管理通过id查询单个数据", sn = "shop:loadById")
    public Shop loadById(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    /**
     * @RequestBody  从请求体中获取数据赋值给shopQuery。
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
//    @PreAuthorize(name = "店铺管理分页查询", sn = "shop:queryPage")
    public PageList<Shop> queryPage(@RequestBody BaseQuery query){
        try {
            return shopService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Shop>();
        }
    }


    /**
     * 删除
     * @DeleteMapping("/{id}") 后端接收前端参数
     * @PathVariable("id")  从资源路径上获取参数id，在赋值给Long id
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
//    @PreAuthorize(name = "店铺管理删除操作", sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
//    @PreAuthorize(name = "店铺管理批量删除", sn = "shop:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }

    }




    /**
     * 添加和修改
     * @PutMapping 请求体携带参数
     * @RequestBody  从请求体中获取参数
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
//    @PreAuthorize(name = "店铺管理添加或修改", sn = "shop:saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() != null){
                shopService.update(shop);
            } else {
                shopService.save(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }

    /**
     * 店铺入驻相关
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) { // 业务问题
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()); // 代码问题 e.getMessage()获取问题
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常，稍后重试!");
        }

    }


    /**
     * 审核驳回接口
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "驳回")
    public AjaxResult reject(@RequestBody ShopLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }
    }


    /**
     * 审核通过接口
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过")
    public AjaxResult pass(@RequestBody ShopLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }
    }


    /**
     * 导出
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            List<Shop> list = shopService.loadAll();
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file", required = true)MultipartFile file){
        try {
            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            list.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *  domain，pojo.： 实体类----》跟数据库中的表对应的
     *  query:  做查询参数使用
     *  dto: 接收参数或者传参使用的
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }








}
